
#ifndef _DIST_Q_H
#define _DIST_Q_H 

#include <iostream>
#include "Swap.h"
#include "TwoWayList.h"

// This is a templte for a thread-safe distributed queue template, 
// built upon the two-way-list.  You can replicate it many times thru
// the "Clone" operation, and it maintains an internal reference count
// to safely clean up memory when the last reference is done with
template <class Type>
class DistributedQueue {

public:

	// return the number of items in the queue
	int Length ();
	
	// pull an item off of the head of the queue; return "true" if something was there,
	// false if nothing was pulled off
	bool AtomicRemove (Type &item);

	// add an item to the tail of the queue
	void Add (Type &item);

	// make a clone of the queue (usually so you can give the
	// clone to another thread)
	void Clone (DistributedQueue &cloneMe);

	// and of course, the swap operation
	void Swap (DistributedQueue &withMe);

	// constructor and destructor
	DistributedQueue ();
	~DistributedQueue ();

private:

	int *refCount;
	TwoWayList <Type> *myList;
	pthread_mutex_t *myMutex;
};

#endif
